package moe.launch;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.EventListener;
import java.util.logging.Logger;

import moe.config.Constants;

public class Tray {
	private final static Logger log = Logger.getLogger(Tray.class.getName());

	private boolean trayIconUsed = false;
	private TrayIcon trayIcon;
	private EventListener listener;

	public Tray(EventListener listener) {
		this.listener = listener;
	}

	public void createTray() {
		try {
			boolean supported = SystemTray.isSupported();
			if (!supported) {
				return;
			}

			Font font = loadFont();

			PopupMenu menuConsole = new PopupMenu("MoeServer");

			MenuItem itemBrowse = new MenuItem("Browse");
			itemBrowse.setActionCommand("browse");
			itemBrowse.addActionListener((ActionListener) listener);
			itemBrowse.setFont(font);
			menuConsole.add(itemBrowse);

			MenuItem itemRestart = new MenuItem("Restart");
			itemRestart.setActionCommand("restart");
			itemRestart.addActionListener((ActionListener) listener);
			itemRestart.setFont(font);
			menuConsole.add(itemRestart);
			menuConsole.addSeparator();

			MenuItem itemExit = new MenuItem("Exit");
			itemExit.setActionCommand("exit");
			itemExit.addActionListener((ActionListener) listener);
			itemExit.setFont(font);
			menuConsole.add(itemExit);

			SystemTray tray = SystemTray.getSystemTray();

			Image icon = loadIocn(tray);

			trayIcon = new TrayIcon(icon, "MoeServer", menuConsole);

			trayIcon.addMouseListener((MouseListener) listener);

			tray.add(trayIcon);

			this.trayIconUsed = true;
		} catch (Exception e) {
			log.warning("I/O error creating tray: " + e.getMessage());
			e.printStackTrace();
		}
	}

	private Font loadFont() {
		Font font = null;
		if (System.getProperty("os.name", "").startsWith("Windows")) {
			font = new Font("Dialog", Font.PLAIN, 11);
		} else {
			font = new Font("Dialog", Font.PLAIN, 12);
		}
		return font;
	}

	private Image loadIocn(SystemTray tray) {
		Dimension d = tray.getTrayIconSize();
		String iconFile;
		if (d.width >= 24 && d.height >= 24) {
			iconFile = Constants.ICON_24_FILE;
		} else if (d.width >= 22 && d.height >= 22) {
			iconFile = Constants.ICON_22_FILE;
		} else {
			iconFile = Constants.ICON_FILE;
		}
		InputStream is = null;
		try {
			is = ClassLoader.getSystemResourceAsStream(iconFile);
			byte[] imageData = new byte[is.available()];
			is.read(imageData);
			return Toolkit.getDefaultToolkit().createImage(imageData);
		} catch (IOException e) {
			log.warning("I/O error loading icon: " + e.getMessage());
			return null;
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// ignore
			}
		}
	}

	public void destroy() {
		if (trayIconUsed) {
			try {
				SystemTray.getSystemTray().remove(trayIcon);
			} catch (Exception e) {
				// ignore
			} finally {
				trayIcon = null;
				trayIconUsed = false;
			}
			// System.gc();
		}
	}
}
